package cn.edu.hubu.hubusmartmedicalsystem.service.impl;

import cn.edu.hubu.hubusmartmedicalsystem.entity.Doctor;
import cn.edu.hubu.hubusmartmedicalsystem.mapper.DoctorMapper;
import cn.edu.hubu.hubusmartmedicalsystem.service.DoctorService;
import cn.edu.hubu.hubusmartmedicalsystem.vo.ResultVO;
import cn.hutool.crypto.digest.DigestUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DoctorServiceImpl implements DoctorService {

    @Autowired
    private DoctorMapper doctorMapper;

    // 检查账号是否可用
    @Override
    public ResultVO checkAccount(String account) {
        Doctor doctor = doctorMapper.selectByID(account);  // 假设 selectByID 按照 doctorId 查找
        if (doctor != null) {
            return new ResultVO(500, "账号不可用", account);
        } else {
            return new ResultVO(200, "账号可用", account);
        }
    }

    // 保存医生信息（注册）
    @Override
    public ResultVO saveDoctor(Doctor doctor) {
        if (doctorMapper.selectByID(doctor.getDoctorId()) != null){
            return new ResultVO<>(500,"添加失败，该医生已存在",null);
        }
        String loginPassword = doctor.getDoctorPassword();
        // 使用 MD5 加密密码（注意：MD5 并不算特别安全，生产环境推荐用 bcrypt）
        String newPassword = DigestUtil.md5Hex(loginPassword);
        doctor.setDoctorPassword(newPassword);

        int i = doctorMapper.insertDoctor(doctor);
        if (i > 0) {
            return new ResultVO<>(200, "注册成功！", null);
        } else {
            return new ResultVO(500, "注册失败", null);
        }
    }

    // 医生登录功能
    @Override
    public ResultVO loginDoctor(String account, String password) {
        // 1. 查询医生信息
        Doctor doctor = doctorMapper.selectByID(account);
        if (doctor == null) {
            return new ResultVO(500, "医生账号不存在", null);
        }

        // 2. 校验密码（假设密码是通过 MD5 加密的）
        String encryptedPassword = DigestUtil.md5Hex(password);
        if (!encryptedPassword.equals(doctor.getDoctorPassword())) {
            return new ResultVO(500, "密码错误", null);
        }

        // 3. 密码正确，返回登录成功信息
        return new ResultVO(200, "登录成功", doctor);
    }

    //查询指定id医生
    @Override
    public ResultVO<Doctor> getDoctorById(String doctorId) {
        Doctor doctor=doctorMapper.selectByID(doctorId);
        if(doctor==null){
            return new ResultVO<>(500,"医生不存在",null);
        }
        return new ResultVO<>(200,"查询成功",doctor);
    }

    //查询所有医生
    @Override
    public ResultVO<List<Doctor>> getAllDoctor(){
        List<Doctor>  doctors=doctorMapper.selectAll();
        if(doctors == null || doctors.isEmpty()) {
            return new ResultVO<>(500,"没找到任何医生信息",null);
        }
        return new ResultVO<>(200,"查询成功",doctors);
    }

    //删除指定id医生
    @Override
    public ResultVO<String> deleteById(String doctorId){
        int result=doctorMapper.deleteById(doctorId);
        if(result>0){
            return new ResultVO<>(200,"医生删除成功","null");
        }
        return new ResultVO<>(500,"医生删除失败",null);
    }

}